$(function () {
  document.body.addEventListener(
    "touchmove",
    function (e) {
      // e.preventDefault();
    }, {
      passive: false
    }
  );

  // 当前显示的弹窗
  window.dialogId = "";

  // 鼠标开始坐标
  window.startPoint = {
    x: 0,
    y: 0
  };

  // 鼠标移动坐标
  window.movePoint = {
    x: 0,
    y: 0
  };

  // 元素移动坐标
  window.toPoint = {
    x: 0,
    y: 0
  };

  // 最终坐标
  window.endPoint = {
    x: 0,
    y: 0
  };

  window.gridOffset = $(".grid-wrap").offset();

  // 表格属性
  window.tableParam = {
    left: gridOffset.left,
    top: gridOffset.top,
    right: gridOffset.left + $(".grid-wrap").width(),
    bottom: gridOffset.top + $(".grid-wrap").height(),
    tdWidth: $(".grid-wrap .row-item")
      .eq(0)
      .width() + 7.1,
    tdHeight: $(".grid-wrap .row-item")
      .eq(0)
      .height() + 7.1
  };

  window.moveParam = {
    width: $(".move-ele").width(),
    height: $(".move-ele").height(),
    maxLeft: 0,
    maxRight: 0
  };

  // 添加宠物
  $(".buy").click(function () {
    addAnimal();
  });

  window.addAnimal = function (imgSrc, animalNum) {
    var imgSrc = imgSrc || "./imgs/animal/m1.png";
    var animalNum = animalNum || 1;
    var $this = $(this);
    var t = t || "";
    $this.addClass("pulse animated infinite");
    clearTimeout(t);
    t = null;
    var t = setTimeout(function () {
      $this.removeClass("pulse animated infinite");
    }, 2000);
    var parent = "";
    [
      [0, 1, 2, 3],
      [0, 1, 2, 3],
      [0, 1, 2, 3]
    ].forEach(function (i, index) {
      var cArr = i.filter(function (j, idx) {
        return !$(
          ".move-ele[data-row='" + index + "'][data-col='" + j + "']"
        ).length;
      });
      if (cArr.length && !parent) {
        parent = $(".rows")
          .eq(index)
          .find(".row-item")
          .eq(cArr[0])
          .append(
            '<div class="move-ele" id="m' +
            index +
            cArr[0] +
            '" data-oldcol="' +
            cArr[0] +
            '" data-oldrow="' +
            index +
            '" data-col="' +
            cArr[0] +
            '" data-row="' +
            index +
            '" data-num="' +
            animalNum +
            '">' +
            '<img class="animal" src="' +
            imgSrc +
            '">' +
            '<span class="num">' +
            animalNum +
            "</span>" +
            "</div>"
          );
        addInterval($("#m" + index + cArr[0]));
      }
    });
  };

  // 领取YMD倒计时  秒
  function redCallback() {
    $('.red-name').text('签到')
      .parent()
      .addClass("on");
  }

  TimeDown(5, $('.red-name'), redCallback)

  // 领取金币倒计时
  function yellowCallback() {
    $('.yellow-name').text('领取')
      .parent()
      .addClass("on");
  }

  TimeDown(80, $('.yellow-name'), yellowCallback)
});